global class ArinsoWebServices {
//Method to retrive the survey for given month (Previous)
    webService static ServiceResponse getSurveyResponse(String contactId, String surveyYear, String surveyMonth){
        ServiceResponse serviceResponse = new ServiceResponse();

        try{
        
            //Validate the input data
            if(contactId == null || contactId.length() <= 0){
                serviceResponse.sucess = false;
                serviceResponse.error = 'Contact Id Field not defined';
                serviceResponse.errorCode = 001;
                return serviceResponse; 
            }
            
            String strQuery = null;
            Customer_Satisfaction_Survey__c currentSurvey = null;
            Integer iCtr = 0;
            List <Customer_Satisfaction_Survey__c> lSurvey = null;
            
            if(surveyYear == null || surveyYear.length() == 0 || surveyMonth == null || surveyMonth.length() == 0){
                //Retrive the survey object from database
                strQuery = 'select Survey_Year__c,Survey_Month__c,CustomerResponse1__c,CustomerResponse2__c,CustomerResponse3__c';
                strQuery += ',CustomerResponse4__c,CustomerResponse5__c,CustomerResponse6__c';
                strQuery += ',CustomerResponse7__c,CustomerResponse8__c,CustomerResponse9__c,CustomerResponse10__c,CustomerComments10__c';
                strQuery += ',CustomerResponse11__c,CustomerResponse12__c,CustomerResponse13__c,CustomerResponse14__c,Contact__c';
                strQuery += ' from Customer_Satisfaction_Survey__c';
                strQuery += ' where Contact__c = \'' + contactId + '\'';
                strQuery += ' and CustomerResponse1__c != null';
                strQuery += ' order by Survey_Year__c desc, Survey_Month__c desc limit 6';
                        
                lSurvey = Database.query(strQuery);
                
                if(!lSurvey.isEmpty()){
                    currentSurvey = lSurvey[0];
                }
                iCtr = 1;
            }else if((surveyYear.length() != 4) || (surveyMonth.length() != 1 && surveyMonth.length() != 2)){
                serviceResponse.sucess = false;
                serviceResponse.error = 'Invalid year or month';
                serviceResponse.errorCode = 002;
                return serviceResponse; 
            }else{
                //Retrive the survey object from database
                strQuery = 'select Survey_Year__c,Survey_Month__c,CustomerResponse1__c,CustomerResponse2__c,CustomerResponse3__c';
                strQuery += ',CustomerResponse4__c,CustomerResponse5__c,CustomerResponse6__c';
                strQuery += ',CustomerResponse7__c,CustomerResponse8__c,CustomerResponse9__c,CustomerResponse10__c,CustomerComments10__c';
                strQuery += ',CustomerResponse11__c,CustomerResponse12__c,CustomerResponse13__c,CustomerResponse14__c,Contact__c';
                strQuery += ' from Customer_Satisfaction_Survey__c';
                strQuery += ' where Contact__c = \'' + contactId + '\'';
                strQuery += ' and CustomerResponse1__c != null';
                strQuery += ' and Survey_Month__c = \'' + surveyMonth + '\'';
                strQuery += ' and Survey_Year__c = \'' + surveyYear + '\' limit 1';
                
                lSurvey = Database.query(strQuery);
                
                if(!lSurvey.isEmpty()){
                    currentSurvey = lSurvey[0];             
                    
                    strQuery = 'select Survey_Year__c,Survey_Month__c,CustomerResponse1__c,CustomerResponse2__c,CustomerResponse3__c';
                    strQuery += ',CustomerResponse4__c,CustomerResponse5__c,CustomerResponse6__c';
                    strQuery += ',CustomerResponse7__c,CustomerResponse8__c,CustomerResponse9__c,CustomerResponse10__c,CustomerComments10__c';
                    strQuery += ',CustomerResponse11__c,CustomerResponse12__c,CustomerResponse13__c,CustomerResponse14__c,Contact__c';
                    strQuery += ' from Customer_Satisfaction_Survey__c';
                    strQuery += ' where Contact__c = \'' + contactId + '\'';
                    strQuery += ' and CustomerResponse1__c != null';
                    strQuery += ' and ((Survey_Month__c in (' + getPreviousMonths(surveyMonth) + ')';
                    strQuery += ' and Survey_Year__c = \'' + surveyYear + '\')';
                    strQuery += ' or ( Survey_Year__c = \'' + getPreviousYear(surveyYear) + '\'))';             
                    strQuery += ' order by Survey_Year__c desc, Survey_Month__c desc limit 5';
                    
                    lSurvey = Database.query(strQuery);
                    iCtr = 0;
                }
            }
            
            if(currentSurvey == null){
                serviceResponse.sucess = false;
                serviceResponse.error = 'Survey Response not found.';
                serviceResponse.errorCode = 007;
                return serviceResponse; 
            }else{
                ClientSurveyResponse clientSurveyResponse = new ClientSurveyResponse();
                clientSurveyResponse.response01 = currentSurvey.CustomerResponse1__c.intValue();
                clientSurveyResponse.response02 = currentSurvey.CustomerResponse2__c.intValue();
                clientSurveyResponse.response03 = currentSurvey.CustomerResponse3__c.intValue();
                clientSurveyResponse.response04 = currentSurvey.CustomerResponse4__c.intValue();
                clientSurveyResponse.response05 = currentSurvey.CustomerResponse5__c.intValue();
                clientSurveyResponse.response06 = currentSurvey.CustomerResponse6__c;
                clientSurveyResponse.response07 = currentSurvey.CustomerResponse7__c.intValue();
                clientSurveyResponse.response08 = currentSurvey.CustomerResponse8__c.intValue();
                clientSurveyResponse.response09 = currentSurvey.CustomerResponse9__c;
                clientSurveyResponse.response10 = currentSurvey.CustomerResponse10__c;
                clientSurveyResponse.responseComments10 = currentSurvey.CustomerComments10__c;
                clientSurveyResponse.response11 = currentSurvey.CustomerResponse11__c;
                clientSurveyResponse.response12 = currentSurvey.CustomerResponse12__c;
                clientSurveyResponse.response13 = currentSurvey.CustomerResponse13__c;
                clientSurveyResponse.response14 = currentSurvey.CustomerResponse14__c;
                clientSurveyResponse.currentMonth = currentSurvey.Survey_Month__c;
                clientSurveyResponse.currentYear = currentSurvey.Survey_Year__c;

                List <previousSurvey> tmpPreviousSurveys = new List<previousSurvey>();
                while(lSurvey.size() > iCtr){
                    Customer_Satisfaction_Survey__c tmpSurvey = lSurvey[iCtr];
                    previousSurvey ps = new previousSurvey();
                    ps.surveyMonth = tmpSurvey.Survey_Month__c;
                    ps.surveyYear = tmpSurvey.Survey_Year__c;
                    tmpPreviousSurveys.add(ps);
                    iCtr++;
                }
                clientSurveyResponse.previousSurveys = tmpPreviousSurveys;
                
                serviceResponse.clientSurveyResponse = clientSurveyResponse;
                serviceResponse.sucess = true;
                return serviceResponse; 
            }
        
        }catch(Exception e){
            serviceResponse.sucess = false;
            serviceResponse.errorCode = 006;
            serviceResponse.error = 'Error : ' + e.getMessage();    
        }

        return serviceResponse;
    }
    
    private static String getPreviousYear(String strYear){
        Integer intYear = Integer.valueOf(strYear);
        intYear--;
        return String.valueOf(intYear);
    }

    private static String getPreviousMonths(String strMonth){
        Integer intMonth = Integer.valueOf(strMonth);
        String strRtn = '';
        if(intMonth != 0){
            while(intMonth > 0){
                intMonth--;
                if(!strRtn.equals('')){
                    strRtn += ',';
                }
                
                strRtn += '\'' + getStringValue(intMonth) + '\'';
            }
        }
        return strRtn;
    }

    //Method to save the survey results
    webService static ServiceResponse addSurveyResponse(String contactId, String surveyYear, String surveyMonth, Integer response01, Integer response02, Integer response03, Integer response04, Integer response05, String response06, Integer response07, Integer response08, String response09, String response10, String responseComments10, String response11, String response12, String response13,String response14){
		
		System.debug('contactId: ' + 'surveyYear: ' + surveyYear + 'surveyMonth: ' + surveyMonth + 'response01: ' + response01 + 'response02: ' + response02 + 'response03: '+ response03+ response04+ response05+ response06+ response07+ response08+ response09+ response10+ response11+ response12+ response13+ 'response14: ' + response14);
        ServiceResponse serviceResponse = new ServiceResponse();
        try{
            
            //Validate the input data
            if(contactId == null || contactId.length() <= 0){
                serviceResponse.sucess = false;
                serviceResponse.error = 'Contact Id Field not defined';
                serviceResponse.errorCode = 001;
                return serviceResponse; 
            }
            
            if((surveyYear == null || surveyYear.length() != 4) || surveyMonth == null || (surveyMonth.length() != 1 && surveyMonth.length() != 2)){
                serviceResponse.sucess = false;
                serviceResponse.error = 'Invalid year or month';
                serviceResponse.errorCode = 002;
                return serviceResponse; 
            }
            
            //Check the contact id and registration
            String strQuery = 'select c.Id, c.Email, c.Account.Name from Contact c where Id = \'' + contactId + '\'';
            strQuery += ' and Survey_registered__c = true';
            List <Sobject> lContacts = Database.query(strQuery);
            
            if(lContacts.isEmpty()){
                serviceResponse.sucess = false;
                serviceResponse.error = 'Contact not registered for the survey';
                serviceResponse.errorCode = 004;
                return serviceResponse; 
            }
            
            //Check if already responded to the survey
            strQuery = 'select Id,CustomerResponse1__c from Customer_Satisfaction_Survey__c';
            strQuery += ' where Survey_Month__c = \'' + surveyMonth + '\'';
            strQuery += ' and Survey_Year__c = \'' + surveyYear + '\'';
            strQuery += ' and Contact__c = \'' + contactId + '\'';
            
            List <Customer_Satisfaction_Survey__c> lSurvey = Database.query(strQuery);
            
            Customer_Satisfaction_Survey__c newSurvey;
            
            boolean bInsert = true;
            if(!lSurvey.isEmpty()){
                bInsert = false;
                newSurvey = lSurvey[0];
                if(newSurvey.CustomerResponse1__c != null){
                    serviceResponse.sucess = false;
                    serviceResponse.error = 'Already filled the survey';
                    serviceResponse.errorCode = 005;
                    return serviceResponse; 
                }
            }else{
                newSurvey = new Customer_Satisfaction_Survey__c();
                Contact contact = (Contact)lContacts[0];
                newSurvey.Contact__c = contact.Id;
                newSurvey.Survey_Month__c = surveyMonth;
                newSurvey.Survey_Year__c = surveyYear;
            }           
            //Insert or Update the survey record to the salesforce

            newSurvey.CustomerResponse1__c = response01;
            newSurvey.CustomerResponse2__c = response02;
            newSurvey.CustomerResponse3__c = response03;
            newSurvey.CustomerResponse4__c = response04;
            newSurvey.CustomerResponse5__c = response05;
            newSurvey.CustomerResponse6__c = response06;
            newSurvey.CustomerResponse7__c = response07;
            newSurvey.CustomerResponse8__c = response08;
            newSurvey.CustomerResponse9__c = response09;
            newSurvey.CustomerResponse10__c = response10;
            newSurvey.CustomerComments10__c = responseComments10;
            newSurvey.CustomerResponse11__c = response11;
            newSurvey.CustomerResponse12__c = response12;
            newSurvey.CustomerResponse13__c = response13;
            newSurvey.CustomerResponse14__c = response14;
            
            Database.Saveresult saveResult = null;
            if(bInsert){
                saveResult = Database.insert(newSurvey,false);
            }else{
                saveResult = Database.update(newSurvey,false);
            }
            
            
            if(!saveResult.isSuccess()){
                serviceResponse.sucess = false;
                String strError = '';
                for(Database.Error error:saveResult.getErrors()){
                    strError += error.getMessage();
                }
                serviceResponse.error = strError;
                return serviceResponse;
            }
            
            serviceResponse.sucess = true;
            
        }catch(Exception e){
            serviceResponse.sucess = false;
            serviceResponse.errorCode = 006;
            serviceResponse.error = 'Error : ' + e.getMessage();
        }   
        
        return serviceResponse; 
    }

    private static String getStringValue(Integer intVal){
        if(intVal < 10){
            return '0' + String.valueOf(intVal);
        }
        return String.valueOf(intVal);
    }
    
    global class ServiceResponse{
        webService boolean sucess;
        webService String error;
        webService Integer errorCode;
        webService ClientSurveyResponse clientSurveyResponse;
    }
    
    global class ClientSurveyResponse{
        webService Integer response01;
        webService Integer response02;
        webService Integer response03;
        webService Integer response04;
        webService Integer response05;
        webService String response06;
        webService Integer response07;
        webService Integer response08;
        webService String response09;
        webService String response10;
        webService String responseComments10;
        webService String response11;
        webService String response12;
        webService String response13;
        webService String response14;
        webService String currentMonth;
        webService String currentYear;
        
        webservice List <previousSurvey> previousSurveys;
    }
    
    
    global class previousSurvey{        
        webService String surveyMonth;
        webService String surveyYear;
    }
    
}